﻿/* ************************************************************
 * JUST TEST CODE -- NOT READY FOR PRODUCTION
 * Author:  Roy Penrod, Bryan Solero
 * 
 * Defines an enemy ship.
 * 
 * Implements the JumpFleetGameLib.xMap.IPositionableObject
 * and JumpFleetGameLib.xMap.IUpdatableObject interfaces.
 * 
 * ************************************************************ */

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

// Oak Engine AI
using OakEngine.AI.Steering;
using OakEngine.AI.Kinematic;
using OakEngine.AI.Waypoint;
using MainJumpFleet.SteerBehaviors;

namespace EnemyShip
{
    class EnemyShip : JumpFleetGameLib.xMap.IPositionableObject, JumpFleetGameLib.xMap.IUpdatableObject
    {
        /********************************
         *  AI Code Section
         ********************************/
        // Dynamic Steering
        Oak_Kinematic agentShip;
        Seek agentSeek;
        Oak_Steering Steering;
        SteeringBehavior behavior;

        // Static Steering
        KinematicMovement kinematicMovement;
        KinematicSeek agentKinematicSeek;
        Oak_Waypoint waypoint;

        /**************************************
         **************************************/


        #region MEMBERS

        // holds the static sprite 
        private JumpFleetGameLib.xSprite.StaticSprite _sprite;

        // enemy ship's position
        private Vector2 _currentPosition;

        // flag for whether the enemy ship is visible
        private bool _visible;

        // enemy ship's rotation angle
        private float _rotationAngle;        
        
        // stores the current position of the SectorMap origin
        private Vector2 _currentMapPosition;

        // asteroid's offset within the SectorMap
        private Vector2 _currentMapOffset;

        // Target
        private Vector2 _TargetShip;

        //Distance Sight
        private float _DistanceSight;

        // Distance Stop
        private float _DistanceStop;

        // Speed
        private float _ShipSpeed;

        #endregion


        #region PROPERTIES

        private Vector2 CurrentPosition
        {
            get { return _currentPosition; }

            set 
            {
                _currentPosition = value;
                
                // sets the sprite's DrawPosition property
                _sprite.DrawPosition = value;
            }
        }

        public bool Visible
        {
            get { return _visible; }

            set
            {
                _visible = value;

                // sets the sprite's Visible property
                _sprite.Visible = value;
            }
        }

        public float RotationAngle
        {
            get { return _rotationAngle; }

            set
            {
                _rotationAngle = value;

                // sets the sprite's RotationAmount property
                _sprite.RotationAmount = value;
            }
        }                

        public Vector2 CurrentMapOffset
        {
            get { return _currentMapOffset; }

            set { _currentMapOffset = value; }
        }

        public Vector2 CurrentMapPosition
        {
            set
            {                
                _currentMapPosition = value;

                CurrentPosition = new Vector2(_currentMapPosition.X + _currentMapOffset.X, _currentMapPosition.Y + _currentMapOffset.Y);
            }
        }

        public float Layer
        {
            set { _sprite.Layer = value; }
        }

        public Vector2 Target
        {
            set
            {
                _TargetShip = value;
            }
            get
            {
                return _TargetShip;
            }
        }

        public float DistanceSight
        {
            set { _DistanceSight = value; }
            get { return _DistanceSight; }
        }

        public float DistanceStop
        {
            set { _DistanceStop = value; }
            get { return _DistanceStop; }
        }

        #endregion


        #region CONSTRUCTORS

        public EnemyShip(JumpFleetGameLib.xSprite.StaticSprite sprite, float rotationAngle)
        {
            // assigns the parameters
            _sprite = sprite;
            Visible = false;
            RotationAngle = rotationAngle;
           
            /*************
             * AI
             **************/
            // Dynamic Steering 
            agentShip = new Oak_Kinematic();
            agentShip.Position = _currentMapOffset;
            agentShip.Direction = new Vector2();
            agentShip.AngularVelocity = 0;
            agentShip.Rotation = 2.1f;

            // Temp SPeed
            _ShipSpeed = 50.0f;

            agentSeek = new Seek();
            agentSeek.m_Agent = agentShip;
            agentSeek.m_Target = new Vector2();
            agentSeek.m_fMaxAcceleration = _ShipSpeed;

            behavior = agentSeek;

            // Steer Update
            Steering = new Oak_Steering();

        }

        #endregion


        #region METHODS

        public void Update(GameTime gameTime)
        {
            // left blank intentionally
            // Thank you Roy!!!


            /************************
             * AI Update
             ***************************/
            Steering.ZeroOut();
            agentShip.Position = _currentMapOffset;

            agentSeek.m_Target = _TargetShip;

            agentSeek.UpdateSteering(Steering);
            agentShip.Update(Steering, 0.95f,(float) gameTime.ElapsedGameTime.TotalSeconds);
            agentShip.SetRotationFromVelocity();
            _sprite.RotationAmount = agentShip.Rotation;
            agentShip.TrimSpeed(_ShipSpeed);

            _currentMapOffset = agentShip.Position;
        }

        #endregion

    } // <-- end class
} // <-- end namespace
